package ldh.im.fxgui.util;

import javafx.scene.image.Image;
import ldh.im.fxbase.service.GifViewService;
import ldh.im.fxbase.util.FileUtil;
import ldh.im.fxbase.util.SoftMapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImageUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ImageUtil.class);

    public static String getFileImagePath(String suffix) {
        if (suffix.equals("")) {
            return FileUtil.class.getResource("/imgs/file/file.png").toExternalForm();
        }
        if (FileUtil.DOC_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/DOC.png").toExternalForm();
        } else if (FileUtil.PDF_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/PDF.png").toExternalForm();
        } else if (FileUtil.EXCEL_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/EXCEL.png").toExternalForm();
        } else if (FileUtil.ZIP_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/zip.png").toExternalForm();
        } else if (FileUtil.XML_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/xml.png").toExternalForm();
        } else if (FileUtil.HTML_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/HTML.png").toExternalForm();
        } else if (FileUtil.TXT_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/DOC.png").toExternalForm();
        } else if (FileUtil.MD_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/MD.png").toExternalForm();
        } else if (FileUtil.PPT_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/PPT.png").toExternalForm();
        } else if (FileUtil.VIDEO_SUFFIXS.contains(suffix)) {
            return ImageUtil.class.getResource("/imgs/file/vedio.png").toExternalForm();
        } else {
            return ImageUtil.class.getResource("/imgs/file/file.png").toExternalForm();
        }
    }

    public static double calcFitImageWidth(double width, double imageWidth, double imageHeight) {
        if(width > imageWidth) {
            return imageWidth;
        }
        return width;
    }

    public static double calcFitImageHeight(double width, double imageWidth, double imageHeight) {
        double rate = width / imageWidth;
        return imageHeight * rate;
    }

    public static Image getCacheImage(String path) {
        return getCacheImage(path, false);
    }

    public static boolean isGif(String path) {
        return path.toLowerCase().endsWith("gif");
    }

    public static Image getCacheImage(String path, boolean isCache) {
        Image cacheImage = SoftMapUtil.getWeakCache(path);
        if (cacheImage != null) {
            return cacheImage;
        }
        long time = System.currentTimeMillis();
        cacheImage = new Image(imageFile(path), isCache);
        LOGGER.info("load image time: {}", (System.currentTimeMillis() - time));
        SoftMapUtil.weakCache(path, cacheImage);
        return cacheImage;
    }

    public static Image getFirstGifImage(String path, boolean isCache) {
        Image cacheImage = SoftMapUtil.getWeakCache(path);
        if (cacheImage != null) {
            return cacheImage;
        }

        GifViewService gifViewService = new GifViewService(path);
        Image image = gifViewService.getFirstImage();
        gifViewService.dispose();
        if (isCache) {
            SoftMapUtil.weakCache(path, image);
        }
        return image;
    }

    private static String imageFile(String path) {
        long time = System.currentTimeMillis();
        try {
            File file = new File(path);
            return file.toURI().toURL().toExternalForm();
        } catch (Exception e) {
            LOGGER.error(String.format("%s is path", path), e);
        }
        LOGGER.info("imageFile time: {}", (System.currentTimeMillis() - time));
        return path;
    }
}
